classdef eyetrack_cogent < eyetrackexp
    % This subclass uses cogent as a graphical motor.

    methods
        function start_GM(self)
            % Initializes the graphical motor, i.e., cogent or PTB.

            if self.Eyelink
                Eyelink('Initialize');
            end

            config_display(self.scn.mon,self.scn.res)
            config_keyboard
            start_cogent
            
            % Prepare strings
            clearpict(self.blank)
            clearpict(self.ready)
            clearpict(self.fixation);
            preparestring('Press a key when ready...',self.ready)
            preparestring('Pause',self.pause_str)
            drawpict(self.ready);

            % Fixation cross at small step from start of stimulus
            drawpict(self.ready);
            waitkeydown(inf);
            drawpict(self.blank); % possibly?
        end

        function runScreen(self)

            % Start dialog
            self.startDialog()
            % Preamble
            self.start_GM()
            function [goout,kn]= escapeTrial()
                goout = 0;
                readkeys
                [k,kt,kn]=getkeydown(self.ESC);
                if kn
                    b = self.num_blocks;
                    t = self.num_trials;
                    goout = 1;
                end
                [k,kt,ks]=getkeydown(71);
                if ks;
                    drawpict(self.ready);
                    waitkeydown(inf);
                end
            end
            function checkOccluder()
                % No eye link
                if self.Eyelink == 0; return; end
                if self.occluder.Xbase(b,t) > 3   % i.e. if occ overhangs
                    if self.occluder.X(b,t) < cx
                        %Eyelink('Message', 'OCCLUDER');
                    end
                else
                    if self.occluder.X(b,t) < cx < self.occluder.End(b,t)
                        %Eyelink('Message', 'OCCLUDER');
                    end
                end
            end
            % Careful with this, because of the indexing
            indexes = reshape(1:self.num_blocks * self.num_trials,...
                self.num_blocks,self.num_trials)';
            for trial = indexes(:)'
                [b,t]= ind2sub([self.num_blocks,self.num_trials],trial);
                preparestring('+', self.fixation, self.randomFixation(trial)*self.scn.w/2,...
                    self.fix.y*self.scn.h/2)
                drawpict(self.fixation);

                tic; while toc < self.fix.time/2; end

                if self.Eyelink
                    Eyelink('Openfile', ['s' num2str(self.Epar.Sbj) 'e' ...
                        num2str(self.Epar.Exp) 'b' num2str(b) ...
                        't' num2str(t) '.edf']);        % open a file on the eyetracker
                    Eyelink('StartRecording');
                    display('start')
                    self.sendparameters(trial);
                end
                
                tic; while toc < self.fix.time/2; end
                
                if self.Eyelink;
                    Eyelink ('Message', 'START');
                end
                c = 1;
                dt = 0;
                while dt <= self.cycles
                    %cgsetsprite(self.stimulus)
                    dt = dt+(1/self.FpC(trial));
                    if c && abs(dt - self.jump.frame(trial)) <= 2/self.FpC(trial)
                        dt = dt + self.jump.m(trial);
                        c = 0;
                    end
                    cx = self.stimuli(dt*2*pi)* self.randomFixation(trial)*self.scn.w/2;
                    cgellipse(cx,0,self.stim.size,self.stim.size, self.stim.rgb, 'f');
                    cgrect(...
                        self.occluder.X(trial)+self.occluder.W(trial)/2, ...
                        self.occluder.y,self.occluder.W(trial), ...
                        self.occluder.h, self.occluder.rgb);
                    cgflip(0,0,0);
                    checkOccluder();
                    [goout,kn] = escapeTrial();
                    if goout; break; end
                end
                % Saves file
                if self.Eyelink
                    Eyelink('Closefile');
                    Eyelink('ReceiveFile');
                end
                % Pauses the script and restarts it
                if kn; break; end
                % Transtion between trials
                self.trialTransition();
                if ~mod(t,self.pauses)
                    t0 = drawpict(self.pause_str);
                    waituntil(t0+1000);
                    stop_cogent
                    if self.Eyelink; Eyelink('Shutdown'); end
                    input('Press enter to continue\n');
                    self.start_cogent() % Initializes every thing again
                end

            end
            if self.Eyelink; Eyelink('Shutdown'); end
            stop_cogent
        end

        function trialTransition(self)
            drawpict(self.fixation);
            cgflip(0,0,0);
            tic; while toc < +self.fix.time; end
            clearpict(self.fixation);
            drawpict(self.blank);
            cgflip(0,0,0);
            while toc < +self.fix.time; end
        end
        
    end
end